package com.yfq.springframework.beans.support;

import com.yfq.springframework.beans.BeansException;
import com.yfq.springframework.beans.config.BeanDefinition;

import java.lang.reflect.Constructor;

/**
 * @author 姚凤桥
 * @date 2023/1/8 17:29
 * @description
 */
public class SimpleInstantiationStrategy implements InstantiationStrategy {
    @Override
    public Object instantiate(String beanName, BeanDefinition beanDefinition,
                              Constructor<?> constructor, Object... args) {
        try {
            if (constructor != null) {
                return constructor.newInstance(args);
            } else {
                return beanDefinition.getBeanClass().newInstance();
            }
        } catch (Exception e) {
            throw new BeansException("创建名为" + beanName + "的bean失:" + e.getMessage());
        }
    }

}
